<?php
namespace app\zhonghang\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cache;

require_once (APP_PATH . 'api/RewardApi.php');
class Coupon extends Base
{
    public function _initialize(){
        parent::_initialize();

    }
    /*
     * 获取购买优惠券列表
     */
    public function getBuyCouponList(){
        $member = $this->member;
        $where['status'] = 1;
        $list = Db::name('coupon')->where($where)->order('price','asc')->select();
        $re['info'] = '查询成功';
        $re['status'] = 1;
        $re['data'] = $list;
        return json($re);
    }


    /*
     * 购买优惠券验证是否剩余购买次数
     */
    public function getBuyCouponNum(){
        $member = $this->member;
        $config = Db::name('config')->where('name','buy_coupon_num')->find();
        if($member['buy_coupon_num']>=$config['value']){
            $re['info'] = '今日已没有夺宝次数';
            $re['status'] = -1;
        }else{
            $re['info'] = '今日还可以继续夺宝';
            $re['status'] = 1;
        }
        return json($re);
    }




    /*
     * 获取我的优惠券列表
     *  $type  查询的状态值     0未使用   1已使用   2已到期
     */
    public function getMyCouponList(){
        $member = $this->member;
        $where['status'] = input('type');  //状态值
        $where['uid'] = $member['user_id'];  //用户ID
        $list = Db::name('coupon_list')->where($where)->select();
        if($list){
            foreach($list as $k=>$v){
                $coupon = Db::name('coupon')->where('id',$v['cid'])->find();
                $list[$k]['price'] = $coupon['price'];   //优惠券购买价格
                $list[$k]['condition'] = $coupon['condition'];   //优惠券减免条件
                $list[$k]['money'] = $coupon['money'];   //优惠券减免数量
            }
        }
        $re['info'] = '查询成功';
        $re['status'] = 1;
        $re['data'] = $list;
        return json($list);
    }


    /*
     * 获取我的礼品列表
     * $type 礼品类型   0 我的礼品列表    1  我送出的礼品列表    2我收到的礼品列表
     */
    public function myGiftList(){
        $member = $this->member;
        $type = input('type');
        $where['status'] = 0;
        if($type==0){
            $where['user_id'] = $member['user_id'];
            $where['get_user_id'] = 0;
            $where['status'] = 0;
        }
        if($type==1){
            $where['get_user_id'] = $member['user_id'];
        }
        if($type==2){
            $where['user_id'] = $member['user_id'];
            $where['get_user_id'] = array('neq',0);
        }
        $list = Db::name('user_gift')->where($where)->order('get_time','desc')->select();
        if($list){
            foreach($list as $k=>$v){
                $goods = Db::name('goods')->where('goods_id',$v['goods_id'])->find();
                $list[$k]['goods_price'] = $goods['shop_price'];    //商品价格
                $list[$k]['goods_name'] = $goods['goods_name'];     //商品名称
                $list[$k]['goods_pic'] = $goods['original_img'];    //商品小图片
            }
        }
        $re['info'] = '查询成功';
        $re['status'] = 1;
        $re['data'] = $list;
        return json($re);
    }




    /*
     * 分享成功后添加每日购买优惠券次数
     */
    public function addBuyCouponNum(){
        $member = $this->member;
        if($member['is_share']==1){
            $re['info'] = '今日已获取过分享赠送购买次数';
            $re['status'] = -1;
            return json($re);
        }else{
            $up['is_share'] = 1;
            $up['buy_coupon_num'] = $member['buy_coupon_num'] + 1;   //增加一个购买机会
            $res = Db::name('users')->where('user_id',$member['user_id'])->update($up);
            if($res !== false){
                $re['info'] = '分享成功,获得一次购买优惠券的机会';
                $re['status'] = 1;
                return json($re);
            }else{
                $re['info'] = '操作失败';
                $re['status'] = 0;
                return json($re);
            }
        }
    }

















}